{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linear Regression Using TensorFlow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (rrule.py, line 55)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"/home/amanullahtariq/anaconda3/lib/python3.5/site-packages/dateutil/rrule.py\"\u001b[0;36m, line \u001b[0;32m55\u001b[0m\n\u001b[0;31m    raise ValueError, \"Can't create weekday with n == 0\"\u001b[0m\n\u001b[0m                    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.patches as mpatches\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_data = np.random.rand(100).astype(np.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Equation for Y = 3X + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_data = 3 * x_data + 2\n",
    "y_data = np.vectorize(lambda y: y + np.random.normal(loc=0.0,scale=0.1))(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "zipped = zip(x_data,y_data)\n",
    "\n",
    "print(zipped)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(1.0)\n",
    "b = tf.Variable(0.2)\n",
    "y = w * x_data + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "loss = tf.reduce_mean(tf.square(y - y_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "optimizer = tf.train.GradientDescentOptimizer(0.5)\n",
    "train = optimizer.minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "sess = tf.Session()\n",
    "sess.run(init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_data = []\n",
    "for step in range(1000):\n",
    "    evals = sess.run([train, w, b])[1:]\n",
    "    if step % 50 == 0:\n",
    "        print(step,evals)\n",
    "        train_data.append(evals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "converter = plt.colors \n",
    "cr,cg, cb = (1.0,1.0,0.0)\n",
    "for f in train_data:\n",
    "    cb += 1.0 / len(train_data)\n",
    "    cg -= 1.0 / len(train_data)\n",
    "    if cb > 1.0 : cb = 1.0\n",
    "    if cg < 0.0 : cg = 0.0\n",
    "    [w,b] = f\n",
    "    \n",
    "    f_y = np.vectorize(lambda x: w*x + b) (x_data)\n",
    "    line = plt.plot(x_data,f_y)\n",
    "    plt.setp(line,color=(cr,cg,cb))\n",
    "    \n",
    "plt.plot(x_data,y_data,'ro')\n",
    "\n",
    "green_line = mpatches.Patch(color='red', label='Data Points')\n",
    "plt.legend(handles=[green_line])\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
